package com.java.cloud.consumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.java.cloud.bookstore.commons.util.JsonResult;
import com.java.cloud.bookstore.commons.util.ResultTool;
import com.java.cloud.consumer.handler.BlockDemoHandler;
import com.java.cloud.consumer.service.Consumer8001Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/consumer1")
public class Consumer8001Controller {
    @Resource
    private Consumer8001Service consumer8001Service;

    @GetMapping("/f")
    public JsonResult a() {
        return consumer8001Service.a();
    }

    @GetMapping("/f/{name}")
    public JsonResult b(@PathVariable("name") String name) {
        return consumer8001Service.b(name);
    }
    @GetMapping("/ribbon")
    public JsonResult c() {
        return consumer8001Service.c();
    }

    @GetMapping("/ribbon/{name}")
    public JsonResult d(@PathVariable("name") String name) {
        return consumer8001Service.d(name);
    }

    @GetMapping("/c")
    public JsonResult c1() {
        return consumer8001Service.c1();
    }

    @GetMapping("/d")
    public JsonResult d1() {
        return consumer8001Service.d1();
    }

    @GetMapping("/e")
    public JsonResult e1() {
        return consumer8001Service.e1();
    }

    @GetMapping("/f1")
    @SentinelResource("messageF")
    public JsonResult f1(int a) {
        System.out.println("a:" + a);
        return consumer8001Service.d1();
    }

    @GetMapping("/g")
    public JsonResult g1(String serviceName) {
        System.out.println("param:" + serviceName);
        return consumer8001Service.d1();
    }

    @GetMapping("/h1")
    @SentinelResource(value = "h1", blockHandler = "abc", fallback = "abcd",blockHandlerClass = {BlockDemoHandler.class})
    public JsonResult h1() {
        return ResultTool.success("success");
    }

    public JsonResult abc(BlockException e) {
        System.out.println("abc:" + e.getMessage());
        return ResultTool.fail();
    }

    public JsonResult abcd(Throwable e) {
        System.out.println("abcd:" + e.getMessage());
        return ResultTool.fail();
    }

}
